package view;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;

import tools.Tab;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class EditTabView extends javax.swing.JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JPanel editTabPanel;
	private JLabel songNameLabel;
	private JButton cancelButton;
	private JButton saveButton;
	private JTextField numRatedTextField;
	private JTextField ratingTextField;
	private JTextField tabTypeTextField;
	private JTextField songNameTextField;
	private JTextField bandNameTextField;
	private JLabel numRatedLabel;
	private JLabel ratingLabel;
	private JLabel tabTypeLabel;
	private JLabel bandNameLabel;
	private View parent;
	private Tab tab;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public EditTabView(View p, Tab t) {
		super();
		parent = p;
		tab = t;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				editTabPanel = new JPanel();
				getContentPane().add(editTabPanel, BorderLayout.CENTER);
				editTabPanel.setLayout(null);
				this.setLocationRelativeTo(null);
				this.setPreferredSize(new java.awt.Dimension(332, 253));
				this.setTitle("Edit Tab Information");
				this.setAlwaysOnTop(true);
				{
					bandNameLabel = new JLabel();
					editTabPanel.add(bandNameLabel);
					bandNameLabel.setText("Artist");
					bandNameLabel.setBounds(12, 19, 92, 15);
				}
				{
					songNameLabel = new JLabel();
					editTabPanel.add(songNameLabel);
					songNameLabel.setText("Song");
					songNameLabel.setBounds(12, 49, 92, 15);
				}
				{
					tabTypeLabel = new JLabel();
					editTabPanel.add(tabTypeLabel);
					tabTypeLabel.setText("Tab Type");
					tabTypeLabel.setBounds(12, 79, 92, 15);
				}
				{
					ratingLabel = new JLabel();
					editTabPanel.add(ratingLabel);
					ratingLabel.setText("Rating");
					ratingLabel.setBounds(12, 109, 92, 15);
				}
				{
					numRatedLabel = new JLabel();
					editTabPanel.add(numRatedLabel);
					numRatedLabel.setText("# of Ratings");
					numRatedLabel.setBounds(12, 139, 92, 15);
				}
				{
					bandNameTextField = new JTextField();
					editTabPanel.add(bandNameTextField);
					bandNameTextField.setBounds(109, 16, 201, 22);
					bandNameTextField.setText(tab.getBandName());
				}
				{
					songNameTextField = new JTextField();
					editTabPanel.add(songNameTextField);
					songNameTextField.setBounds(109, 46, 201, 22);
					songNameTextField.setText(tab.getSongName());
				}
				{
					tabTypeTextField = new JTextField();
					editTabPanel.add(tabTypeTextField);
					tabTypeTextField.setBounds(109, 76, 94, 22);
					tabTypeTextField.setText(tab.getTabType());
				}
				{
					ratingTextField = new JTextField();
					editTabPanel.add(ratingTextField);
					ratingTextField.setBounds(109, 106, 94, 22);
					ratingTextField.setText(tab.getRating());
				}
				{
					numRatedTextField = new JTextField();
					editTabPanel.add(numRatedTextField);
					numRatedTextField.setBounds(109, 136, 71, 22);
					numRatedTextField.setText(tab.getNumRated());
				}
				{
					saveButton = new JButton();
					editTabPanel.add(saveButton);
					saveButton.setText("Save Changes");
					saveButton.setBounds(12, 190, 152, 22);
					saveButton.addActionListener(this);
					saveButton.setActionCommand("saveButton");
				}
				{
					cancelButton = new JButton();
					editTabPanel.add(cancelButton);
					cancelButton.setText("Cancel");
					cancelButton.setBounds(213, 190, 98, 22);
					cancelButton.addActionListener(this);
					cancelButton.setActionCommand("cancelButton");
				}
			}
			pack();
			this.setSize(332, 253);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if( arg0.getActionCommand().equals("saveButton") )	{
			String bn = bandNameTextField.getText();
			String sn = songNameTextField.getText();

			if( !bn.equals(tab.getBandName()) || !sn.equals(tab.getSongName()) )	{
				parent.deleteTab(tab.getReference());
			}
			
			tab.setBandName(bn);
			tab.setSongName(sn);
			tab.setTabType(tabTypeTextField.getText());
			tab.setRating(ratingTextField.getText());
			tab.setNumRated(numRatedTextField.getText());
			if( parent.saveTab(tab) )	{
				this.dispose();
			}
			else	{
				JOptionPane.showMessageDialog(this, "Couldn't update tab entry!");
			}
		}
		else if( arg0.getActionCommand().equals("cancelButton") )	{
			this.dispose();
		}
		
	}

}
